package com.zlf.controller;

import com.zlf.entity.UserEntity;
import com.zlf.service.UserService;
import com.zlf.util.VerifyCodeUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author zlf
 * @Date 2021/5/24 10:18
 */
@Controller
@RequestMapping("/user")
public class UserController {

    /*
    用户登录
     */
    @RequestMapping("/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password,
                        @RequestParam("verifyCode") String verifyCode, HttpSession session) {
        String verifyCodes = (String) session.getAttribute("verifyCode"); // 校验验证码
        Subject subject = SecurityUtils.getSubject(); // 获取当前登录用户
        try {
            if (verifyCodes.equalsIgnoreCase(verifyCode)) {
                subject.login(new UsernamePasswordToken(username, password)); // 执行登录操作
                return "redirect:/index.jsp"; // 认证通过后直接跳转到index.jsp
            } else {
                throw new RuntimeException("验证码错误");
            }
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("用户名错误~");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("密码错误~");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/login.jsp"; // 如果认证失败仍然回到登录页面
    }

    /*
    用户退出
     */
    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login.jsp"; // 退出后仍然会到登录页面
    }

    @Autowired
    private UserService userService;

    /*
    用户注册
     */
    @RequestMapping("/register")
    public String register(UserEntity user) {
        try {
            userService.register(user);
            return "redirect:/login.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/register.jsp";
    }

    /*
    生成验证码图片
     */
    @RequestMapping("/getImage")
    public void getImage(HttpSession session, HttpServletResponse response) throws IOException {
        //生成验证码
        String verifyCode = VerifyCodeUtil.generateVerifyCode(4);
        //验证码放入session
        session.setAttribute("verifyCode", verifyCode);
        //验证码存入图片
        ServletOutputStream os = response.getOutputStream();
        response.setContentType("image/png");
        VerifyCodeUtil.outputImage(180, 40, os, verifyCode);
    }

}
